//
//  PK-ios
//
//  Created by peikua on 15/9/15.
//  Copyright (c) 2015年 peikua. All rights reserved.
//

#import "UIView+Category.h"
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>

@implementation UIView (Category)

-(void) addToWindow
{
    id appDelegate = [[UIApplication sharedApplication] delegate];
    if ([appDelegate respondsToSelector:@selector(window)])
    {
        UIWindow * window = (UIWindow *) [appDelegate performSelector:@selector(window)];
        [window addSubview:self];
    }
}

+(void)allBtnDeSelected:(UIView *)fatherView onlyOneSelected:(UIButton *)selectedBtn{
    if (fatherView.subviews.count > 0) {
        for (id inView in fatherView.subviews) {
            if ([inView isKindOfClass:[UIButton class]]) {
                UIButton *allBtns = (UIButton *)inView;
                if (allBtns != selectedBtn) {
                    allBtns.selectedRed = false;
                }
            }
        }
    }
}

+(instancetype)createTopBottomLineViewWithBacgroundColor:(UIColor *)color Frame:(CGRect)frame{
    UIView *topBottomLineView = [[UIView alloc]initWithFrame:frame];
//    NSLog(@"frame==>%@",NSStringFromCGRect(frame));
    topBottomLineView.backgroundColor = color;
    
    UIView *topLineView = [UIView lineViewWithTop:true Position:topBottomLineView.bounds.origin.y Color:nil];
    UIView *bottomLineView = [UIView lineViewWithTop:false Position:topBottomLineView.bounds.size.height Color:nil];
    [topBottomLineView addSubview:topLineView];
    [topBottomLineView addSubview:bottomLineView];
    
    return topBottomLineView;
}

+(instancetype)createTopLineViewWithBacgroundColor:(UIColor *)color Frame:(CGRect)frame{
    UIView *topBottomLineView = [[UIView alloc]initWithFrame:frame];
    //    NSLog(@"frame==>%@",NSStringFromCGRect(frame));
    topBottomLineView.backgroundColor = color;
    
    UIView *topLineView = [UIView lineViewWithTop:true Position:topBottomLineView.bounds.origin.y Color:nil];
    
    [topBottomLineView addSubview:topLineView];
    
    
    return topBottomLineView;
}


-(void)setAddBottomLineView:(BOOL)addBottomLineView{
    if (addBottomLineView) {
        UIView *bottomLineView = [UIView lineViewWithTop:false Position:self.bounds.size.height Color:nil];
        [self addSubview:bottomLineView];
    }
}

-(void)setAddTopLineView:(BOOL)addTopLineView{
    if (addTopLineView) {
        UIView *topLineView = [UIView lineViewWithTop:true Position:self.bounds.origin.y Color:nil];
        [self addSubview:topLineView];
    }
}

+(void)allBtnDeSelected:(UIView *)fatherView onlyOneSelectedWithTag:(NSInteger )selectedBtnTag{
    if (fatherView.subviews.count > 0) {
        for (id inView in fatherView.subviews) {
            if ([inView isKindOfClass:[UIButton class]]) {
                UIButton *allBtns = (UIButton *)inView;
                allBtns.selectedRed  =false;
                if (allBtns.tag == selectedBtnTag) {
                    allBtns.selectedRed = true;
                }
            }
        }
    }
}

+(void)allBtnDeSelected:(UIView *)fatherView{
    if (fatherView.subviews.count > 0) {
        for (id inView in fatherView.subviews) {
            if ([inView isKindOfClass:[UIButton class]]) {
                UIButton *allBtns = (UIButton *)inView;
                    allBtns.selectedRed = false;
            }
        }
    }
}

@end


@implementation UIView (Screenshot)

- (UIImage*) screenshot {
    
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // helps w/ our colors when blurring
    // feel free to adjust jpeg quality (lower = higher perf)
    NSData *imageData = UIImageJPEGRepresentation(image, 0.75);
    image = [UIImage imageWithData:imageData];
    
    return image;
    
}

- (UIImage *) screenshotForScrollViewWithContentOffset:(CGPoint)contentOffset {
    UIGraphicsBeginImageContext(self.bounds.size);
    //need to translate the context down to the current visible portion of the scrollview
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0f, -contentOffset.y);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // helps w/ our colors when blurring
    // feel free to adjust jpeg quality (lower = higher perf)
    NSData *imageData = UIImageJPEGRepresentation(image, 0.55);
    image = [UIImage imageWithData:imageData];
    
    return image;
}

- (UIImage*) screenshotInFrame:(CGRect)frame {
    UIGraphicsBeginImageContext(frame.size);
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), frame.origin.x, frame.origin.y);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // helps w/ our colors when blurring
    // feel free to adjust jpeg quality (lower = higher perf)
    NSData *imageData = UIImageJPEGRepresentation(image, 0.75);
    image = [UIImage imageWithData:imageData];
    
    return image;
}

-(UIImage *) convertToImage {
    CGSize viewSize = self.bounds.size;
    UIGraphicsBeginImageContextWithOptions(viewSize, NO, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

@implementation UIView (Animation)

- (void) shakeAnimation {
    
    CALayer* layer = [self layer];
    CGPoint position = [layer position];
    CGPoint y = CGPointMake(position.x - 8.0f, position.y);
    CGPoint x = CGPointMake(position.x + 8.0f, position.y);
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction
                                  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    [animation setAutoreverses:YES];
    [animation setDuration:0.08f];
    [animation setRepeatCount:3];
    [layer addAnimation:animation forKey:nil];
}

- (void) trans180DegreeAnimation
{
    [UIView animateWithDuration:0.3 animations:^{
        self.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
    }];
}

+(instancetype)lineViewWithTop:(BOOL)top Position:(CGFloat)position Color:(UIColor *)color{
    
    UIView *lineView = [[UIView alloc]init];
    lineView.size = CGSizeMake(kScreenWidth, 1);
    if (top) {
        [lineView setTop:position];
    }else{
        [lineView setBottom:position];
    }
    
    if (color) {
        lineView.backgroundColor = color;
    }else{
        lineView.backgroundColor = [UIColor hex:@"e6e6e6"];
    }
    
    
    return lineView;
}

+(instancetype)separateBottomLineWithColor:(UIColor *)color{
    UIView *sepacateLine = [[UIView alloc]init];
    
    if (color) {
        sepacateLine.backgroundColor = color;
    }else{
        sepacateLine.backgroundColor = [UIColor hex:@"e6e6e6"];
    }
    
    sepacateLine.size = CGSizeMake(kScreenWidth, 1);
    return sepacateLine;
}

@end

